//package com.colorfruit.framework.rabbitmq.component;
//
//
//import com.colorfruit.framework.common.idgenerate.BizNumberType;
//import com.colorfruit.framework.common.idgenerate.DateTimeIdGenerator;
//import com.colorfruit.framework.rabbitmq.config.RabbitMQConstant;
//import com.colorfruit.framework.rabbitmq.model.BusinessMsg;
//import com.colorfruit.framework.rabbitmq.model.ProducerMsg;
//import org.springframework.amqp.rabbit.support.CorrelationData;
//import org.springframework.beans.a_factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//@Component
//public class ProducerAckComponent {
//
//    @Autowired
//    private RedisMqComponent redisMqComponent;
//
//    /**
//     * 生成CorrelationData，以便消息发送后进行确认
//     * @param message
//     * @param producerName
//     * @return
//     */
//    public CorrelationData generateCorrelation(BusinessMsg<String, Object> message, String producerName) {
//
//        CorrelationData correlationData = generateCorrelationData();
//
//        save(correlationData, message, producerName);
//
//        return correlationData;
//    }
//
//    /**
//     * 生成确认id
//     * @return
//     */
//    private CorrelationData generateCorrelationData() {
//        String correlationDataId = DateTimeIdGenerator.nextNo(BizNumberType.CARD_CHANNEL);
//        CorrelationData correlationData = new CorrelationData(correlationDataId);
//        return correlationData;
//    }
//
//    /**
//     * 保存correlation与消息的对应关系
//     * @param correlationData
//     * @param message
//     * @param producerName
//     */
//    private void save(CorrelationData correlationData, BusinessMsg<String, Object> message, String producerName) {
//        ProducerMsg producerMsg = new ProducerMsg(producerName, message);
//        redisMqComponent.saveObj(RabbitMQConstant.LAST_SEND_MSG  + producerName, correlationData.getId());
//        redisMqComponent.saveObj(correlationData.getId(), producerMsg);//为节省内存，可将此数据保存的结构调整为hash结构
//    }
//
//    /**
//     * 确认成功，则将correlationDataId对应的cache删除
//     * @param correlationDataId
//     */
//    public void ackSuccessResolve(String producerName, String correlationDataId){
//        redisMqComponent.increment(RabbitMQConstant.ACK_SUCCESS + producerName);
//        redisMqComponent.delete(correlationDataId);
//    }
//
//    /**
//     * 处理确认失败的情况
//     * @param correlationDataId
//     */
//    public void ackFailedResolve(String producerName, String correlationDataId){
//        redisMqComponent.increment(RabbitMQConstant.ACK_FAIL + producerName);
//        ProducerMsg sendFailMsg = (ProducerMsg) redisMqComponent.getObj(correlationDataId);
//        sendFailMsg.setCallBacked(true);
//        redisMqComponent.saveWithList( RabbitMQConstant.ACK_FAIL_LIST + producerName, sendFailMsg);
//        redisMqComponent.delete(correlationDataId);
//    }
//}
